package cn.sytton.taffecloud.service.system.controller;

import cn.sytton.taffecloud.common.base.doc.*;
import cn.sytton.taffecloud.common.db.model.PageParam;
import cn.sytton.taffecloud.common.excel.annotation.ExportExcel;
import cn.sytton.taffecloud.service.system.entity.Auth;
import cn.sytton.taffecloud.service.system.entity.RoleEntity;
import cn.sytton.taffecloud.service.system.entity.RoleUserEntity;
import cn.sytton.taffecloud.service.system.entity.UserEntity;
import cn.sytton.taffecloud.service.system.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 角色控制层
 *
 * @author skyrock
 */
@Api(tags = "角色")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private IRoleService roleService;

    @ApiOperation("查询分页角色列表")
    @ApiImplicitParam(name = "pp", value = "分页参数对象", paramType = ParamType.query, dataTypeClass = PageParam.class)
    @ExportExcel
    @GetMapping
    public PageInfo<RoleEntity> findListPage(PageParam pp) {
        return roleService.findListPage(pp);
    }

    @ApiOperation("查询单个角色")
    @ApiImplicitParam(name = "id", value = "角色ID", paramType = ParamType.path)
    @GetMapping("/{id}")
    public RoleEntity findById(@PathVariable String id) {
        return roleService.findById(id);
    }

    @ApiOperation("添加角色")
    @ApiImplicitParam(name = "entity", value = "角色数据对象", paramType = ParamType.body, dataTypeClass = RoleEntity.class)
    @PostMapping
    public void create(@RequestBody @Validated RoleEntity entity) {
        roleService.create(entity);
    }

    @ApiOperation("更新角色")
    @ApiImplicitParam(name = "entity", value = "角色数据对象", paramType = ParamType.body, dataTypeClass = RoleEntity.class)
    @PostMapping("/update")
    public void update(@RequestBody @Validated RoleEntity entity) {
        roleService.update(entity);
    }

    @ApiOperation("删除角色")
    @ApiImplicitParam(name = "ids", value = "角色ID数组", paramType = ParamType.body, dataTypeClass = String[].class)
    @PostMapping("/delete")
    public void delete(@RequestBody String[] ids) {
        roleService.delete(ids);
    }

    @ApiOperation("获取角色的授权菜单ID及功能ID")
    @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = ParamType.query)
    @GetMapping("/menu")
    public Map<String, Object> findMenu(@RequestParam String roleId) {
        return roleService.findMenu(roleId);
    }

    @ApiOperation(value = "授权菜单及权限")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = ParamType.body, name = "roleId", value = "角色ID", required = true),
            @ApiImplicitParam(paramType = ParamType.body, name = "trueNodeIds", value = "添加授权的菜单ID数组", dataTypeClass = String[].class),
            @ApiImplicitParam(paramType = ParamType.body, name = "trueFuncKeys", value = "添加授权的功能ID数组", dataTypeClass = String[].class),
            @ApiImplicitParam(paramType = ParamType.body, name = "falseNodeIds", value = "取消授权的菜单ID数组", dataTypeClass = String[].class),
            @ApiImplicitParam(paramType = ParamType.body, name = "falseFuncKeys", value = "取消授权的功能ID数组", dataTypeClass = String[].class)
    })
    @PostMapping("/menu/auth")
    public void authMenu(@RequestBody Auth auth) {
        roleService.authMenu(auth);
    }

    @ApiOperation("查询用户分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = ParamType.path),
            @ApiImplicitParam(name = "pp", value = "分页参数对象", paramType = ParamType.query, dataTypeClass = PageParam.class)
    })
    @GetMapping("/user/{roleId}")
    public PageInfo<UserEntity> findUserListPage(@PathVariable String roleId, PageParam pp) {
        return roleService.findUserListPage(roleId, pp);
    }

    @ApiOperation("添加角色用户")
    @ApiImplicitParam(name = "entity", value = "角色用户对象 roleId, userIds", paramType = ParamType.body, dataTypeClass = RoleUserEntity.class)
    @PostMapping("/user/add")
    public void addUsers(@RequestBody RoleUserEntity entity) {
        roleService.addUsers(entity);
    }

    @ApiOperation("移除角色用户")
    @ApiImplicitParam(name = "entity", value = "角色用户对象 roleId, userIds", paramType = ParamType.body, dataTypeClass = RoleUserEntity.class)
    @PostMapping("/user/remove")
    public void removeUsers(@RequestBody RoleUserEntity entity) {
        roleService.removeUsers(entity);
    }

    @ApiOperation("修改j角色状态")
    @ApiImplicitParam(name = "entity", value = "角色数据对象", paramType = ParamType.body, dataTypeClass = RoleEntity.class)
    @PostMapping("/status/update")
    public void updateRoleStatus(@RequestBody RoleEntity entity) {
        roleService.updateRoleStatus(entity);
    }

    @ApiOperation(value = "导入角色", notes = "导入请求样例,返回导入结果文件名")
    @ApiImplicitParam(name = "file", value = "导入Excel文件", paramType = ParamType.body, dataTypeClass = MultipartFile.class)
    @PostMapping("/import")
    public String importData(@RequestBody MultipartFile file) {
        return roleService.importData(file);
    }
}
